//
//  HomeTableViewCell.m
//  CYWFrame
//
//  Created by Macx on 2017/10/23.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "HomeTableViewCell.h"

#import <Masonry.h>

#import "Posts.h"
#import "LZPictureBrowser.h"
#import "PushPerHome.h"

@implementation HomeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self CreatUI];
    }
    
    return self;
}

- (void)CreatUI
{
    
    _headPic = [[UIImageView alloc] init];
    [self.contentView addSubview:_headPic];
    [_headPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.mas_offset(10);
        make.width.and.height.mas_offset(SW(9));
    }];
    _headPic.layer.cornerRadius = SW(18);
    _headPic.layer.masksToBounds = YES;
    
    _level = [[UIImageView alloc] init];
    [self.contentView addSubview:_level];
    [_level mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_offset(_headPic.width/3*2);
//        make.left.mas_offset(_headPic.width/3*2);
        make.bottom.mas_equalTo(_headPic.mas_bottom);
        make.right.mas_equalTo(_headPic.mas_right);
        make.width.and.height.mas_offset(SW(27));
    }];
    
    _isfine = [[UILabel alloc] init];
    [self.contentView addSubview:_isfine];
    _isfine.textColor = kRedColor;
    _isfine.font = kUHSystemFontWithSize(16);
    _isfine.layer.borderColor = kRedColor.CGColor;
    _isfine.layer.borderWidth = 1;
    _isfine.layer.cornerRadius = 3;
    _isfine.layer.masksToBounds = YES;
    _isfine.text = @"置顶";
    _isfine.textAlignment = NSTextAlignmentCenter;
    CGSize fineS = [LZCTools textSizeWithText:@"字数" font:_isfine.font];
    [_isfine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headPic.mas_top).mas_offset(0);
        make.left.mas_equalTo(_headPic.mas_right).mas_offset(10);
        make.height.mas_offset(SW(16));
        make.width.mas_offset(fineS.width+fineS.width/4);
    }];
               
    
    _nickName = [[UILabel alloc] init];
    [self.contentView addSubview:_nickName];
    _nickName.textColor = kBlackColor;
    _nickName.font = kUHSystemFontWithSize(16);
    [_nickName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headPic.mas_top).mas_offset(0);
        make.left.mas_equalTo(_isfine.mas_right).mas_offset(10);
        make.height.mas_offset(SW(16));
    }];

    _gender = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_gender];
    [_gender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nickName.mas_top).mas_offset(0);
        make.left.mas_equalTo(_nickName.mas_right).mas_offset(5);
        make.size.mas_equalTo(CGSizeMake(SW(18), SW(18)));
    }];
    
    _date = [[UILabel alloc]init];
    _date.textColor = kLightGrayColor;
    _date.font = kUHSystemFontWithSize(12);
    [self.contentView addSubview:_date];
    [_date mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nickName.mas_bottom).mas_offset(0);
        make.left.mas_equalTo(_headPic.mas_right).mas_offset(10);
        make.height.mas_offset(SW(18));
    }];
    
//    _collection = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.contentView addSubview:_collection];
//    [_collection mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_date.mas_top).mas_offset(0);
//        make.left.mas_equalTo(_date.mas_right).mas_offset(10);
//        make.height.mas_offset(SW(18));
//        make.width.mas_offset(70);
//    }];
//    _collection.titleLabel.font = kUHSystemFontWithSize(12);
//    [_collection setTitleColor:kGrayColor forState:UIControlStateNormal];
    
    _views = [[UILabel alloc] init];
    [self.contentView addSubview:_views];
    [_views mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_date.mas_top).mas_offset(0);
        make.left.mas_equalTo(_date.mas_right).mas_offset(10);
        make.height.mas_offset(SW(18));
        make.width.mas_offset(170);
    }];
    _views.font = kUHSystemFontWithSize(12);
    _views.textColor = kGrayColor;
    
    CGSize size3 = [LZCTools textSizeWithText:@"未关注" font:kUHSystemFontWithSize(13)];
    CGFloat focusWidth = 10+size3.width+size3.height;
    _focus = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_focus];
    _focus.titleLabel.font = kUHSystemFontWithSize(13);
    [_focus setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [_focus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-10);
        make.size.mas_equalTo(CGSizeMake(focusWidth, size3.height+8));
        make.centerY.mas_equalTo(_headPic.mas_centerY);
    }];
    
    
    _mood = [[UILabel alloc]init];
    _mood.textColor = kBlackColor;
    _mood.font = kUHSystemFontWithSize(15);
    [self.contentView addSubview:_mood];
    _mood.numberOfLines = 2;
    [_mood mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headPic.mas_bottom).mas_offset(10);
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
    }];
    
    _topic = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_topic];
    _topic.titleLabel.font = kUHSystemFontWithSize(13);
    [_topic setTitleColor:kWhiteColor forState:UIControlStateNormal];
//    [_topic setImage:IMAGE(@"icon_topic") forState:UIControlStateNormal];
    [_topic setTitleColor:kGrayColor forState:UIControlStateNormal];
    [_topic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_mood.mas_bottom).mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_offset(0);
        make.width.mas_offset(0);
    }];
    
    _bgView = [[UIView alloc] init];
    [self.contentView addSubview:_bgView];
//    _bgView.backgroundColor = kRedColor;
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_topic.mas_bottom).mas_offset(10);
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_offset(0);
    }];
    
    
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i = 0; i < 9; i++) {
        UIImageView *img = [[UIImageView alloc] init];
        [_bgView addSubview:img];
        img.hidden = YES;
        img.contentMode = UIViewContentModeScaleAspectFit;
        [arr addObject:img];
    }
    self.imgArr = [NSArray arrayWithArray:arr];
    
    
    _line = [[UIView alloc] init];
    [self.contentView addSubview:_line];
    _line.backgroundColor = UISeparatedColorRGB;
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_bgView.mas_bottom).mas_offset(0);
        make.width.mas_offset(SCREEN_WIDTH);
        make.height.mas_offset(1);
    }];
    
    _money = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_money];
    [_money setImage:[IMAGE(@"icon_reward") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_money setTitle:@" 打赏" forState:UIControlStateNormal];
    [_money setTitleColor:kBlackColor forState:UIControlStateNormal];
    _money.titleLabel.font = kUHSystemFontWithSize(13);
    [_money mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_line.mas_bottom).mas_offset(0);
        make.left.mas_offset(0);
        make.size.mas_equalTo(CGSizeMake(SW(3), SW(18)+20));
    }];
    
    _praise = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_praise];
    [_praise setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_praise setTitleColor:kBlackColor forState:UIControlStateNormal];
    [_praise setTitle:@" 110" forState:UIControlStateNormal];
    _praise.titleLabel.font = kUHSystemFontWithSize(13);
    [_praise mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_line.mas_bottom).mas_offset(10);
        make.top.mas_equalTo(_line.mas_bottom).mas_offset(0);
        make.left.mas_equalTo(_money.mas_right).offset(0);
        make.size.mas_equalTo(CGSizeMake(SW(3), SW(18)+20));
    }];
    
    _comment = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_comment];
    [_comment setImage:[IMAGE(@"icon_comment") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//    [_comment setTitle:@" 评论" forState:UIControlStateNormal];
    [_comment setTitleColor:kBlackColor forState:UIControlStateNormal];
    _comment.titleLabel.font = kUHSystemFontWithSize(13);
    [_comment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_line.mas_bottom).mas_offset(0);
        make.left.mas_equalTo(_praise.mas_right).offset(0);
        make.size.mas_equalTo(CGSizeMake(SW(3), SW(18)+20));
    }];
    
    int state = [LZCTools thinkEnv];
    if (state == 0) {
        _money.hidden = YES;
    }
    
    _sepView = [[UIView alloc] init];
    [self.contentView addSubview:_sepView];
    _sepView.backgroundColor = UISeparatedColorRGB;
    [_sepView mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.top.mas_equalTo(_praise.mas_bottom).mas_offset(10);
        make.top.mas_equalTo(_praise.mas_bottom).mas_offset(0);
        make.height.mas_offset(10);
        make.left.offset(0);
        make.width.mas_offset(SCREEN_WIDTH);
        make.bottom.mas_offset(0);
    }];

}

- (void)setModel:(Posts *)model {
    if (_model != model) {
        _model = model;
        
    
        [self.headPic sd_setImageWithURL:[NSURL URLWithString:[model.author valueForKey:@"face"]] placeholderImage:IMAGE(@"placeHolder")];
        
        [self.headPic addActionWithblock:^{
            
            [PushPerHome pushPerHomeWithUserInfo:[model valueForKey:@"author"]];
            
        }];

        if (model.is_fine == 1 && self.type) {
            CGSize fineS = [LZCTools textSizeWithText:@"字数" font:_isfine.font];
            [_isfine mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_offset(fineS.width+fineS.width/4);
            }];
            [_nickName mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(_isfine.mas_right).mas_offset(10);
            }];
        }
        else {
            [_isfine mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_offset(0);
            }];
            [_nickName mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(_isfine.mas_right).mas_offset(0);
            }];
        }
        
        self.nickName.text = [model.author valueForKey:@"nickname"];
        if (self.nickName.text.length > 8) {
            self.nickName.text = [self.nickName.text substringToIndex:8];
        }
//        self.nickName.text = [NSString stringWithFormat:@"%ld", model.post_id];
        if ([[model.author valueForKey:@"sex"] intValue] == 1) {
            [self.gender setImage:IMAGE(@"sex_boy") forState:UIControlStateNormal];
        }
        else
            [self.gender setImage:IMAGE(@"sex_girl") forState:UIControlStateNormal];
    
        if ([[model.author valueForKey:@"role"] isEqualToString:@"1"]) {
            self.level.image = IMAGE(@"");
        }
        else if ([[model.author valueForKey:@"role"] isEqualToString:@"2"]) {
            self.level.image = IMAGE(@"icon_add_v");
        }
        else if ([[model.author valueForKey:@"role"] isEqualToString:@"3"]) {
            self.level.image = IMAGE(@"icon_add_v_expert");
        }
        
        
        self.date.text = model.human_time;
        
        
//        if (model.is_favorite == 1) {
//            [self.collection setImage:IMAGE(@"icon_collect_ed") forState:UIControlStateNormal];
//            [self.collection setTitle:@"已收藏" forState:UIControlStateNormal];
//        }
//        else {
//            [self.collection setImage:IMAGE(@"icon_collect") forState:UIControlStateNormal];
//            [self.collection setTitle:@"收藏" forState:UIControlStateNormal];
//        }
        
        if (model.views >= 10000) {
            _views.text = [NSString stringWithFormat:@"浏览 %ld 万次",model.views/10000];
        }else {
            _views.text = [NSString stringWithFormat:@"浏览 %ld 次",model.views];
        }

        
        if (model.is_zan == 1) {
            [self.praise setImage:[IMAGE(@"index_zan_ed") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        }
        else
            [self.praise setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
        [self.praise setTitle:[NSString stringWithFormat:@" %ld", model.zan_num] forState:UIControlStateNormal];
        
        [self.comment setTitle:[NSString stringWithFormat:@" %ld", model.reply_num] forState:UIControlStateNormal];
        
        if ([[model.author valueForKey:@"is_like"] intValue] == 1) {
            [self.focus setBackgroundImage:IMAGE(@"btn_concerned_bg") forState:UIControlStateNormal];
            [self.focus setImage:[IMAGE(@"btn_ed_concern") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [self.focus setTitle:@"已关注" forState:UIControlStateNormal];
        }
        else {
            [self.focus setBackgroundImage:IMAGE(@"btn_unconcern_bg") forState:UIControlStateNormal];
            [self.focus setImage:[IMAGE(@"btn_un_concern") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [self.focus setTitle:@"  关注" forState:UIControlStateNormal];
        }
        if ([[GetUserDefaule(USERINFO) valueForKey:@"user_id"] isEqualToString:[model.author valueForKey:@"user_id"]]) {
            //            self.focus.hidden = YES;
            [self.focus setTitle:@"删除" forState:UIControlStateNormal];
            [self.focus setTitleColor:kRedColor forState:UIControlStateNormal];
            [self.focus setBackgroundImage:IMAGE(@"") forState:UIControlStateNormal];
            [self.focus setImage:[IMAGE(@"") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        }
        else {
            self.focus.hidden = NO;
            [self.focus setTitleColor:kWhiteColor forState:UIControlStateNormal];
        }
        
        self.mood.text = model.details;
        NSMutableAttributedString *lsStr = [[NSMutableAttributedString alloc] initWithString:self.mood.text];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:4];//调整行间距
        [lsStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.mood.text length])];
        self.mood.attributedText = lsStr;
        
//        self.mood.text = model.details;
//        self.mood.adjustsFontSizeToFitWidth = true;
//        self.mood.lineBreakMode = NSLineBreakByTruncatingTail;
        
        if (!model.tag) {
            [_topic mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(_mood.mas_bottom).mas_offset(0);
                make.height.mas_offset(0);
                make.width.mas_offset(0);
            }];
        }
        else {
            [_topic setTitle:[NSString stringWithFormat:@"# %@", model.tag[@"name"]] forState:UIControlStateNormal];
            CGSize s = [LZCTools textSizeWithText:_topic.titleLabel.text font:_topic.titleLabel.font];
            [_topic mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(_mood.mas_bottom).mas_offset(10);
                make.height.mas_offset(s.height);
                make.width.mas_offset(s.width+s.height*2);
            }];
        }
        
        
        
        if (model.is_zan == 1) {
            [self.praise setImage:[IMAGE(@"index_zan_ed") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        }
        else
            [self.praise setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
        [self.praise setTitle:[NSString stringWithFormat:@" %ld", model.zan_num] forState:UIControlStateNormal];
        
        if (model.is_rewarded == 1) {
            [self.money setTitle:@"已打赏" forState:UIControlStateNormal];
            [self.money setTitleColor:kRedColor forState:UIControlStateNormal];
        }
        else {
            [self.money setTitle:@"打赏" forState:UIControlStateNormal];
            [self.money setTitleColor:kBlackColor forState:UIControlStateNormal];
        }
        
        // 每次赋值清空
//        _videoImgView.image = IMAGE(@"");
        _videoImgView.hidden = YES;
        for (UIImageView *imgView in self.imgArr) {
//            imgView.image = [UIImage imageNamed:@""];
            imgView.hidden = YES;
        }

        if (model.pic.count > 0) {
            CGFloat imgW = (SCREEN_WIDTH-24)/3;
            
            self.mArr = [[NSMutableArray alloc] init];
            NSMutableArray *arr = [[NSMutableArray alloc] init];
            for (NSDictionary *dict in model.pic) {
                [self.mArr addObject:[dict valueForKey:@"origin"]];
                [arr addObject:[dict valueForKey:@"thumbnail"]];
            }
            CGFloat Y = 0.0;
            for (int i = 0; i < arr.count; i++) {
                
//                UIImageView *imgView = [[UIImageView alloc] init];
                //                [_bgView addSubview:imgView];
                UIImageView *imgView = self.imgArr[i];
                
                imgView.hidden = NO;
                _videoImgView.clipsToBounds = YES;
                [imgView addTarget:self action:@selector(imageClick:)];
                [imgView sd_setImageWithURL:[NSURL URLWithString:arr[i]] placeholderImage:IMAGE(@"placeHolder")];
                imgView.tag = 1+i;
                imgView.frame = CGRectMake((imgW+2)*(i%3), (imgW+2)*(i/3), imgW, imgW);
                Y = imgView.y+imgView.height;
            }
            [_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_offset(Y+10);
            }];
            //            [self updateConstraintsIfNeeded];
//            _videoImgView.image = IMAGE(@"");
            _videoImgView.hidden = YES;
        }
        
        else if (model.type == 2) {
            _videoImgView = [[UIImageView alloc] init];
            _videoImgView.hidden = NO;
//            _videoImgView.tag = 101;
            _bgView.tag = 101;
            [_videoImgView sd_setImageWithURL:[NSURL URLWithString:model.cover_url] placeholderImage:IMAGE(@"placeHolder")];
            _videoImgView.clipsToBounds = YES;
            _videoImgView.contentMode = UIViewContentModeScaleAspectFit;
            _videoImgView.frame = CGRectMake(0, 0, SCREEN_WIDTH-20, (SCREEN_WIDTH-20)*9/16);
            [_bgView addSubview:_videoImgView];
            
            CGFloat btnH = _videoImgView.height/4;
            self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_videoImgView addSubview:self.playBtn];
            [self.playBtn setBackgroundImage:IMAGE(@"icon_play") forState:UIControlStateNormal];
            self.playBtn.frame = CGRectMake(0, 0, btnH, btnH);
            self.playBtn.center = CGPointMake(_videoImgView.centerX, _videoImgView.height/2);
//            self.playBtn.userInteractionEnabled = NO;
            
            
            [_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_offset((SCREEN_WIDTH-20)*9/16+10);
            }];
            
            for (UIImageView *imgView in self.imgArr) {
//                imgView.image = [UIImage imageNamed:@""];
                imgView.hidden = YES;
            }
        }
        else {
//            _videoImgView.image = IMAGE(@"");
            _videoImgView.hidden = YES;
            for (UIImageView *imgView in self.imgArr) {
//                imgView.image = [UIImage imageNamed:@""];
                imgView.hidden = YES;
            }
//            [_bgView removeAllSubViews];
            [_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_offset(0);
            }];
        }
        [self updateConstraints];
    }
}

- (void)imageClick:(UIImageView *)img {
    
    UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer
                                             *)img;
    
    LZPictureBrowser *mvc = [[LZPictureBrowser alloc] init];
    [mvc showWithPictureURLs:self.mArr atIndex:[tapRecognizer.view tag]-1];
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
